﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Configuration;

namespace Nano.Core.Config
{
    public class ConfigEngine : INotifyPropertyChanged
    {
        private Configuration config;
        private AppSettingsSection appSettings;
        private static ConfigEngine singletonInstance = null;

        public static ConfigEngine Instance
        {
            get
            {
                if (singletonInstance == null)
                {
                    singletonInstance = new ConfigEngine();
                }

                return singletonInstance;
            }
        }

        public ConfigEngine()
        {
            config = WebConfigurationManager.OpenWebConfiguration("/");
            appSettings = config.AppSettings;
        }

        public string WebSiteTitle
        {
            get
            {
                return appSettings.Settings["WebSiteTitle"].Value;
            }
            set
            {
                if (appSettings.Settings["WebSiteTitle"].Value != value)
                {
                    appSettings.Settings["WebSiteTitle"].Value = value;
                    OnPropertyChanged("WebSiteTitle");
                }
            }
        }

        public string WebSiteDescription
        {
            get
            {
                return appSettings.Settings["WebSiteDescription"].Value;
            }
            set
            {
                if (appSettings.Settings["WebSiteDescription"].Value != value)
                {
                    appSettings.Settings["WebSiteDescription"].Value = value;
                    OnPropertyChanged("WebSiteDescription");
                }
            }
        }

        public string Layout
        {
            get
            {
                return appSettings.Settings["Layout"].Value;
            }
            set
            {
                if (appSettings.Settings["Layout"].Value != value)
                {
                    appSettings.Settings["Layout"].Value = value;
                    OnPropertyChanged("Layout");
                }
            }
        }

        public bool IsConfigured
        {
            get
            {
                return bool.Parse(appSettings.Settings["IsConfigured"].Value);
            }
            set
            {
                if (bool.Parse(appSettings.Settings["IsConfigured"].Value) != value)
                {
                    appSettings.Settings["IsConfigured"].Value = value.ToString();
                    OnPropertyChanged("IsConfigured");
                }
            }
        }

        public ExtensionElementCollection Extensions
        {
            get
            {
                var extensions = config.GetSection("appExtensions");

                return ((ExtensionsSection)extensions).Extensions;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }

            try
            {
                config.Save();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}